/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.admin;

import dao.LoaiSanPhamDao;
import javax.faces.context.FacesContext;
import pojo.Loaisanpham;

/**
 *
 * @author Alvis
 */
public class Admin_ProductTypeController {

    /** Creates a new instance of Admin_ProductTypeController */
    String search;
    private Loaisanpham[] listLoaiSanPham;
    private Loaisanpham loaiSanPhamTemp = new Loaisanpham();
    public Admin_ProductTypeController() {
        getProductTypeList();
    }

    /**
     * @return the loaiSanPhamTemp
     */
    public Loaisanpham getLoaiSanPhamTemp() {
        return loaiSanPhamTemp;
    }

    /**
     * @param loaiSanPhamTemp the loaiSanPhamTemp to set
     */
    public void setLoaiSanPhamTemp(Loaisanpham loaiSanPhamTemp) {
        this.loaiSanPhamTemp = loaiSanPhamTemp;
    }
    
    public String InsertProductType()
    {
        LoaiSanPhamDao loaiSanPhamDAO = new LoaiSanPhamDao();
        Loaisanpham value = new Loaisanpham();
        value.setTenloaisanpham(loaiSanPhamTemp.getTenloaisanpham());
        value.setMota(loaiSanPhamTemp.getMota());
        loaiSanPhamDAO.Insert(value);
        return "indexproducttype";
    }
    public String UpdateProductType()
    {
        LoaiSanPhamDao loaiSanPhamDAO = new LoaiSanPhamDao();
        Loaisanpham value = new Loaisanpham();
        value.setTenloaisanpham(loaiSanPhamTemp.getTenloaisanpham());
        value.setMota(loaiSanPhamTemp.getMota());
        loaiSanPhamDAO.Update(value,loaiSanPhamTemp.getMaloaisanpham());
        return "indexproducttype";
    }
    public String DeleteProductType()
    {
        LoaiSanPhamDao loaiSanPhamDAO = new LoaiSanPhamDao();
        loaiSanPhamDAO.Delete(loaiSanPhamTemp.getMaloaisanpham());
        return "indexproducttype";
    }
    public String getProductTypeUpdate() {
        LoaiSanPhamDao loaiSanPhamDAO = new LoaiSanPhamDao();
        setLoaiSanPhamTemp(loaiSanPhamDAO.getProductType(Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("txtID").toString())));
        return "updateproducttype";

    }
    public String getProductTypeDelete() {
        LoaiSanPhamDao loaiSanPhamDAO = new LoaiSanPhamDao();
        setLoaiSanPhamTemp(loaiSanPhamDAO.getProductType(Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("txtID").toString())));
        return "deleteproducttype";

    }
    
    public String getProductTypeList() {
        
        try {
            LoaiSanPhamDao loaiSanPhamDAO = new LoaiSanPhamDao();
            listLoaiSanPham = loaiSanPhamDAO.getAllProductType();

        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            
        }
        return "indexproducttype";

    }
    public String SearchProductType() {
        
        search =FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("searchstring").toString();
        try {
            LoaiSanPhamDao loaiSanPhamDAO = new LoaiSanPhamDao();
            if(search.equals(""))
            listLoaiSanPham = loaiSanPhamDAO.getAllProductType();
            else
                listLoaiSanPham = loaiSanPhamDAO.SearchProductType(search);
                
         

        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            
        }
        return "indexproducttype";

    }

    /**
     * @return the listLoaiSanPham
     */
    public Loaisanpham[] getListLoaiSanPham() {
        return listLoaiSanPham;
    }
    
}
